﻿using AutoMapper;
using DevProtocolApi.Dtos.system;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.EntityModel.SysModel;
using Service.IService.ISysService;

namespace DevProtocolApi.Controllers.sysControllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LocationController : BaseController
    {
        private readonly ILocationService _locationService;
        private readonly ILogger<LocationController> _logger;
        private readonly IMapper _mapper;

        public LocationController(
            ILocationService locationService,
            ILogger<LocationController> logger,
            IMapper mapper)
        {
            _locationService = locationService;
            _logger = logger;
            _mapper = mapper;
        }

        [HttpPost]
       
        public IActionResult Add(LocationMasterDto dto)
        {
            try
            {
                var location = _mapper.Map<LocationMaster>(dto);
                string id = _locationService.AddLocation(location);
                return !string.IsNullOrEmpty(id) ? Success($"添加成功 {id}") : Failed("添加失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"添加区域失败: {dto.Name}");
                return Failed($"添加失败: {ex.Message}");
            }
        }

        [HttpPut]
       
        public IActionResult Update(LocationMasterDto dto)
        {
            try
            {
                var location = _mapper.Map<LocationMaster>(dto);
                bool success = _locationService.UpdateLocation(location);
                return success ? Success("更新成功") : Failed("更新失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"更新区域失败: {dto.Id}");
                return Failed($"更新失败: {ex.Message}");
            }
        }

        [HttpDelete("{locationId}")]
       
        public IActionResult Delete(string locationId)
        {
            try
            {
                bool success = _locationService.DeleteLocation(locationId);
                return success ? Success("删除成功") : Failed("删除失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"删除区域失败: {locationId}");
                return Failed($"删除失败: {ex.Message}");
            }
        }

        [HttpGet("{locationId}")]
       
        public IActionResult GetById(string locationId)
        {
            try
            {
                var location = _locationService.GetLocationById(locationId);
                return location != null ? Success(location) : Failed("区域不存在");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取区域信息失败: {locationId}");
                return Failed($"获取失败: {ex.Message}");
            }
        }

        [HttpGet]
       
        public IActionResult GetAll()
        {
            try
            {
                var locations = _locationService.GetAllLocations();
                return Success(locations);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取所有区域失败");
                return Failed($"获取失败: {ex.Message}");
            }
        }

        [HttpGet("byArea/{area}")]
       
        public IActionResult GetByArea(string area)
        {
            try
            {
                var locations = _locationService.GetLocationsByArea(area);
                return Success(locations);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取大区区域失败: {area}");
                return Failed($"获取失败: {ex.Message}");
            }
        }

        [HttpPost("updateArea/{locationId}")]
       
        public IActionResult UpdateArea(string locationId, [FromBody] UpdateAreaDto dto)
        {
            try
            {
                bool success = _locationService.UpdateLocationArea(locationId, dto.NewArea);
                return success ? Success("大区更新成功") : Failed("大区更新失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"更新区域大区失败: {locationId}");
                return Failed($"更新失败: {ex.Message}");
            }
        }

        [HttpGet("search")]
       
        public IActionResult Search([FromQuery] string keyword)
        {
            try
            {
                var locations = _locationService.SearchLocations(keyword);
                return Success(locations);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"搜索区域失败: {keyword}");
                return Failed($"搜索失败: {ex.Message}");
            }
        }
    }
}
